const db = require('../db/index')
const bcrypt =require('bcryptjs')


//注册逻辑处理
exports.register = (req,res) => {
     const userinfo = req.body
     userinfo.password = bcrypt.hashSync(userinfo.password,10)
     const  sql = 'insert into user set ?'
     db.query(sql,{username:userinfo.username,password:userinfo.password,grade:userinfo.grade,sno:userinfo.sno,class:userinfo.class,role:'user'}).then(([results,fields])=>{
          res.send({message:"注册成功!!!",code:200})
     }).catch(reason => {
          res.send(reason)
     })
}

//登录逻辑处理
exports.login = (req,res) => {
     const userinfo=req.body
     const sql = 'select * from user where username = ?'
     db.query(sql,userinfo.username).then(([results,fields])=>{
          const user= results[0]
          if(user){
               const compareResult=bcrypt.compareSync(userinfo.password,user.password)
               if(compareResult && userinfo.role===user.role){
                    return  res.send({message:'登录成功!!!',data: user,code:200})
               }
          }
          return  res.send({message:'登录失败!!!',code:403})
     }).catch((reason)=>{
          res.send(reason)
     })
}

//获取用户信息逻辑处理
exports.userList = (req,res) => {
    const sql = 'select * from user where role = ?'
     db.query(sql,'user').then(([result])=>{
          res.send(result)
     }).catch(reason => {
          res.send(reason)
     })
}

//修改用户信息逻辑处理
exports.userUpdate = (req,res) => {

}